package edu.uct.tamnd.viettourism.placesdetail;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import edu.uct.tamnd.urlimageviewhelper.UrlImageViewHelper;
import edu.uct.tamnd.viettourism.R;
import edu.uct.tamnd.viettourism.helper.DatabaseHelper;
import edu.uct.tamnd.viettourism.maps.MapsNew;
import edu.uct.tamnd.viettourism.places.TableNamePlaces;

public class PlacesDetail extends Activity{

	int placesID;
	private String TAG = "Places Detail";
	DatabaseHelper mHelper;
	private String address;
	private String name;
	private String image;
	private String description;
	private String longitude;
	private String latitude;
	
	Cursor cursor;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.places_detail);
		
		
		//imageLoader = new ImageLoader(context);
		//ScrollView scv = (ScrollView) findViewById(R.id.scrollView);
		ImageView imv = (ImageView) findViewById(R.id.imvPlacesDetailImage);
		TextView tvContent = (TextView) findViewById(R.id.tvContent);
		TextView tvAddress = (TextView) findViewById(R.id.tvAddress);

		registerForContextMenu(imv);
		registerForContextMenu(tvContent);
		registerForContextMenu(tvAddress);
		
		Bundle bundle = getIntent().getExtras();
		placesID = bundle.getInt("placesID");
		Log.d(TAG , "Nhan placesID = " + placesID);
		openDatabase();
		loadInfo();	
		
		Button btnChiaSe = (Button) findViewById(R.id.btnChiaSe);
		Button btnViewOnMaps = (Button) findViewById(R.id.btnViewOnMaps);
				
		btnChiaSe.setOnClickListener(new OnClickListener() {
						
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//v.showContextMenu();
				Intent shareIntent=new Intent(Intent.ACTION_SEND);
		        shareIntent.setType("text/plain");
		        shareIntent.putExtra(Intent.EXTRA_TEXT, name + "\n" + description);
		        //shareIntent.putExtra(Intent.EXTRA_SUBJECT, Description);
		        startActivity(Intent.createChooser(shareIntent, "Share..."));
						        
			}
		});
		
		btnViewOnMaps.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(PlacesDetail.this, MapsNew.class);
				Bundle bundle = new Bundle();
				bundle.putString("lat", latitude);
				bundle.putString("lng", longitude);
				bundle.putString("desID", String.valueOf(placesID));
				intent.putExtras(bundle);
				startActivity(intent);
				
				
//				final Intent intent = new Intent(Intent.ACTION_VIEW,
//					    Uri.parse(
//					            "http://maps.google.com/maps?" +
//					            "saddr=21.030333,105.782356&daddr="+latitude+","+longitude));
//					         intent.setClassName(
//					          "com.google.android.apps.maps",
//					          "com.google.android.maps.MapsActivity");
//					   startActivity(intent);
			}
		});

	}
	

	private void loadInfo() {
		// TODO Auto-generated method stub
		cursor = mHelper.readPlacesDetail(placesID);
		
		if (cursor.moveToFirst()) {
			do {
				address = cursor.getString(cursor.getColumnIndex(TableNamePlaces.COLUMN_ADDRESS));
				name = cursor.getString(cursor.getColumnIndex(TableNamePlaces.COLUMN_NAME));
				image = cursor.getString(cursor.getColumnIndex(TableNamePlaces.COLUMN_IMAGE));
				description = cursor.getString(cursor.getColumnIndex(TableNamePlaces.COLUMN_DES));
				longitude = cursor.getString(cursor.getColumnIndex(TableNamePlaces.COLUMN_LONG));
				latitude = cursor.getString(cursor.getColumnIndex(TableNamePlaces.COLUMN_LAT));

			} while (cursor.moveToNext());
		}
 		ImageView imvPlacesDetailImage = (ImageView) findViewById(R.id.imvPlacesDetailImage);
		//imageLoader.DisplayImage(image, imvPlacesDetailImage);
		UrlImageViewHelper.setUrlDrawable(imvPlacesDetailImage, image, R.drawable.loading);		
		TextView tvAddress = (TextView) findViewById(R.id.tvAddress);
		tvAddress.setText("Địa chỉ: " + address);
		TextView tvContent = (TextView) findViewById(R.id.tvContent);
		tvContent.setText(description);
		setTitle(name);

	}


	private void openDatabase(){
		mHelper = new DatabaseHelper(this);
		try {
			mHelper.createDatabase();
			Log.d(TAG, "Đã gọi hàm create");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			Log.d("CityList", e.getMessage());
			Toast.makeText(this, "Lỗi db", Toast.LENGTH_LONG).show();
		}
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		getMenuInflater().inflate(R.menu.context_detail_places, menu);
		
		menu.setHeaderTitle("Xem chi tiết hơn tại");  
		menu.setHeaderIcon(android.R.drawable.ic_dialog_alert);
//        menu.add(0, v.getId(), 0, "Wikipedia").setIcon(R.drawable.google_png);  
//        menu.add(0, v.getId(), 0, "Google").setIcon(this.getResources().getDrawable(R.drawable.google_png));
        
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch(item.getItemId()) {
		case R.id.menu_wiki:
			item.setIcon(R.drawable.wiki);
			Intent wikiSearch;
			wikiSearch = new Intent(Intent.ACTION_VIEW, Uri.parse("https://vi.wikipedia.org/wiki/Special:Search?search=" + name + "&go=Go"));
			startActivity(wikiSearch);
			
			return true;
			
		case R.id.menu_google:
			item.setIcon(R.drawable.google_png);
			Intent googleSearch = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com/search?q=" + name));
			startActivity(googleSearch);
			
			return true;
		
	}
	return super.onContextItemSelected(item);
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		
	    return super.onCreateOptionsMenu(menu);
	}


	

}
